package com.huhu.controller;

import com.github.pagehelper.PageInfo;
import com.huhu.dto.Result;
import com.huhu.entity.Remote;
import com.huhu.service.inter.NGService;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * Created by xuyi on 2016/12/29.
 */
@Controller
@RequestMapping("/ng")
public class NGController {

    @Autowired
    private NGService ngService;

    @ResponseBody
    @RequestMapping(value = "/testJrebel", method = RequestMethod.GET)
    public Result testJrebel() {
        return Result.ok("testJrebel--------");
    }

    @ResponseBody
    @RequestMapping(value = "/testRlock1", method = RequestMethod.GET)
    public Result testRlock1() {
        Config config = new Config();
        config.useSingleServer().setAddress("192.168.31.100:6379");
        RedissonClient redissonClient = Redisson.create(config);
        RLock test_lock = redissonClient.getLock("test_lock");
        test_lock.lock(30L, TimeUnit.SECONDS);
        //  lock方法空参的话 ，过期时间会被重置，即无限时长
        //        test_lock.lock();
        try {
            System.out.println("-------------ass");
        }
        finally {
//            test_lock.unlock();
        }
        return Result.ok("ok");
    }

    @ResponseBody
    @RequestMapping(value = "/testRlock2", method = RequestMethod.GET)
    public Result testRlock2() {

        RedissonClient redissonClient = Redisson.create();
        RLock test_lock = redissonClient.getLock("test_lock");
        test_lock.lock(30L, TimeUnit.SECONDS);
        try {
            System.out.println("testRlock_____________2");
        }
        finally {
            test_lock.unlock();
        }
        return Result.ok("ok");
    }


    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result login(String username, String password) {

        return Result.ok(1);
    }

    @ResponseBody
    @RequestMapping(value = "/findRemote", method = RequestMethod.GET)
    public Result findRemote(String startDate, String endDate,
                             @RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer rows) {
        PageInfo<Remote> remotePageInfo = ngService.findRemote(startDate, endDate, page, rows);
        return Result.ok(remotePageInfo);
    }

    @ResponseBody
    @RequestMapping(value = "/delRemoteCache", method = RequestMethod.GET)
    public Result delRemoteCache(@RequestParam(defaultValue = "1") Integer page,
                             @RequestParam(defaultValue = "10") Integer rows) {
        int flag =  ngService.delRemoteCache(page, rows);
        return Result.ok(flag);
    }



}
